/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.common.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author Arisgo
 * @since 2023/12/14
 */
public class StringUtil {

    public static String firstCharToLower(String value) {
        char firstChar = value.charAt(0);
        if (Character.isUpperCase(firstChar)) {
            return String.valueOf(Character.toLowerCase(firstChar)).concat(value.substring(1));
        }
        return value;
    }


    /**
     * 第一个字符转换为大写
     *
     * @param value 字符串
     */
    public static String firstCharToUpper(String value) {
        char firstChar = value.charAt(0);
        if (Character.isLowerCase(firstChar)) {
            return String.valueOf(Character.toUpperCase(firstChar)).concat(value.substring(1));
        }
        return value;
    }


    /**
     * 驼峰转下划线格式
     *
     * @param value 字符串
     */
    public static String camelToUnderline(String value) {
        if (isBlank(value)) {
            return value;
        }
        int length = value.length();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            char c = value.charAt(i);
            if (Character.isUpperCase(c) && i > 0) {
                sb.append('_').append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 下划线转驼峰格式
     *
     * @param value 字符串
     */
    public static String underlineToCamel(String value) {
        if (isBlank(value)) {
            return value;
        }
        String temp = value.toLowerCase();
        int length = temp.length();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            char c = temp.charAt(i);
            if (c == '_') {
                if (++i < length) {
                    sb.append(Character.toUpperCase(temp.charAt(i)));
                }
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static List<String> underlineToCamel(String... values) {
        if (values == null || values.length == 0) {
            return Collections.emptyList();
        }
        List<String> list = new ArrayList<>(values.length);
        for (String value : values) {
            list.add(underlineToCamel(value));
        }
        return list;
    }

    public static boolean isBlank(String value) {
        if (!hasLength(value)) {
            return true;
        }
        return value.trim().isEmpty();
    }

    public static boolean hasLength(String value) {
        return value != null && !value.isEmpty();
    }

    public static boolean isNotBlank(String value) {
        return !isBlank(value);
    }

    /**
     * 这个字符串是否是全是数字
     *
     * @param value 字符串
     */
    public static boolean isNumeric(String value) {
        if (isBlank(value)) {
            return false;
        }
        for (int i = 0; i < value.length(); i++) {
            int c = value.charAt(i);
            if (c < 48 || c > 57) {
                return false;
            }
        }
        return true;
    }

    public static boolean startsWithAny(String str, String... prefixes) {
        if (isBlank(str) || prefixes == null) {
            return false;
        }

        for (String prefix : prefixes) {
            if (str.startsWith(prefix)) {
                return true;
            }
        }
        return false;
    }

    public static boolean endsWithAny(String str, String... suffixes) {
        if (isBlank(str) || suffixes == null) {
            return false;
        }

        for (String suffix : suffixes) {
            if (str.endsWith(suffix)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 正则匹配
     *
     * @param regex 正则
     * @param value 字符串
     */
    public static boolean matches(String regex, String value) {
        if (null == regex || null == value) {
            return false;
        }
        return Pattern.matches(regex, value);
    }

    public static String appendSeparator(String val, String separator) {
        return !endsWithAny(val, separator) ? val + separator : val;
    }

    public static String removeSeparator(String val, String separator) {
        return endsWithAny(val, separator) ? val.substring(0, val.lastIndexOf(separator)) : val;
    }

}
